package com.canbot.u05.activity.shoppingGuide.sales;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.network.SalesApi;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 售卖内容执行界面
 */
public class SellContentActivity extends BaseActivityNoTitle {

        private Button btndelete;

        private android.widget.TextView tvcontenttitle;

        private String title;

        private Dialog dialogLoading;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_sell_content);

                Intent intent = getIntent();
                title = intent.getStringExtra("title");


                // TODO: 2017/3/16 售卖模式
                initView();

                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                //发送胸口执行任务
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://" + IpMsgConst.SOCKET_HOST2 + ":7755")
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(setTimeOut())
                        .build();
                SalesApi salesApi = retrofit.create(SalesApi.class);
                Call<String> stringCall = salesApi.executeSell(title);
                stringCall.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {

                                if (dialogLoading != null){
                                        LoadingDialogUtils.closeDialog(dialogLoading);
                                        ToastTools.newInstance(SellContentActivity.this).showText(getString(R.string.send_successfully));
                                }

                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                                if (dialogLoading != null){
                                        LoadingDialogUtils.closeDialog(dialogLoading);
                                }
                                ToastTools.newInstance(SellContentActivity.this).showText(getString(R.string.send_failed));
                        }
                });


        }

        private void initView() {
                this.tvcontenttitle = (TextView) findViewById(R.id.tv_content_title);
                this.btndelete = (Button) findViewById(R.id.btn_delete);
                tvcontenttitle.setText(title + "");
        }

        /**
         * 返回键
         *
         * @param view
         */
        public void delete(View view){
                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
                //退出执行-发送销毁界面
                MsgSendUtils.sendStringMsg(MsgType.RECEIVER_MSG_FINISH_ACTIVITY,"1");
                finish();
                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
        }
        private static final int TIMEOUT = 100;//超时时间100s
        /**
         * 设置超时
         * @return
         */
        private OkHttpClient setTimeOut(){
                OkHttpClient client = new OkHttpClient.Builder()
                        .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
                        .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
                        .readTimeout(TIMEOUT, TimeUnit.SECONDS)
                        .build();
                return client;

        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
                if (dialogLoading != null){
                        dialogLoading.dismiss();
                }
        }
}
